package http

import (
	"errors"
	"gitee.com/simple-set/simple.io/src/event"
	"log"
)

type ResponseProcess struct {
	encode *ResponseEncode
}

func (r *ResponseProcess) Accept(session *event.Session) error {
	return nil
}

func (r *ResponseProcess) Disconnect(session *event.Session) error {
	return nil
}

func (r *ResponseProcess) Active(session *event.Session, value any) (any, error) {
	response, ok := value.(*Response)
	if !ok {
		session.Close()
		return nil, errors.New("not response type")
	}
	if err := r.encode.Encode(response); err != nil {
		session.Close()
		return nil, err
	}

	log.Println(session.Socket.RemoteAddr().String(), response.Proto, response.request.Method, response.statusCode, response.request.Host, response.request.URL.Path)
	return response, nil
}

func NewResponseProcess() *ResponseProcess {
	return &ResponseProcess{encode: &ResponseEncode{}}
}
